/**
 * Created by wangshaohua on 2018/1/25.
 */
moduleContent.controller("AddContentDynamicHtmlForFile", function (UploadFileService, $$CambioConfig, ExerciseContentResource, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        $scope.typeOptions = Enums.DynamicHtmlTypeEnum.options;
        $scope.currentContentDynamicHtml = {
            parentId: $scope.currentParentId,
            picture: LearningMapTemplateUtils.findExercise('H5').picture
        };
        //
        // summer.setValue('<p>32423432423423432423423432feafew</p>');


    });

    // ------------------------------------------------------------------------------------------------------

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        $scope.currentContentDynamicHtml.recordType = Enums.ExerciseContentRecordTypeEnum.CONTENT;
        $scope.currentContentDynamicHtml.space = Enums.ExerciseContentSpaceEnum.DYNAMIC_HTML;
        // if ($('.note-editable')) {
        //     $scope.currentContentDynamicHtml.audioContent = summer.getValue('audioContent');
        // }
        Alert.confirm(function () {
            if ($scope.currentContentDynamicHtml.studyClassType == 'AUDIO') {
                ExerciseContentResource.saveAudio({}, $scope.currentContentDynamicHtml, function (result) {
                    Alert.success();
                    DialogService.close();
                });
            } else {
                ExerciseContentResource.saveDynamicHtml({}, $scope.currentContentDynamicHtml, function (result) {
                    Alert.success();
                    DialogService.close();
                });
            }
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------
    $scope.uploadPicture = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.currentContentDynamicHtml.picture = o.id;
            });
        }
    };

    $scope.uploadImage = function ($files) {
        if ($files && $files[0]) {
            $scope.imageProgress = {};
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope, false, $scope.showImageProgress, $scope.imageProgress).then(function (o) {
                $scope.currentContentDynamicHtml.image = o.id;
            });
        }
    };

    $scope.uploadContentImage = function ($files) {
        if ($files && $files[0]) {
            $scope.contentImageProgress = {};
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope, false, $scope.showContentImageProgress, $scope.contentImageProgress).then(function (o) {
                $scope.currentContentDynamicHtml.contentImage = o.id;
            });
        }
    };

    $scope.uploadBackgroundImage = function ($files) {
        if ($files && $files[0]) {
            $scope.backgroundImageProgress = {};
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope, false, $scope.showBackgroundImageProgress, $scope.backgroundImageProgress).then(function (o) {
                $scope.currentContentDynamicHtml.backgroundImage = o.id;
            });
        }
    };

    $scope.uploadZip = function ($files) {
        $scope.currentContentDynamicHtml.type = 'H5';
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'FILE', $scope).then(function (o) {
                $scope.currentContentDynamicHtml.zip = o.id;
                $scope.currentContentDynamicHtml.zipName = o.originalName;
                $scope.currentContentDynamicHtml.studyClassType = $scope.toStudyClassType(o.originalName);
            });
        }
    };

    var getSuffix = function (fileName) {
        var beginIndex = fileName.lastIndexOf('.');
        return fileName.substr(beginIndex + 1);
    }

    $scope.toStudyClassType = function (fileName) {
        var suffix = getSuffix(fileName).toLowerCase();
        switch (suffix) {
            case 'pdf':
                return Enums.StudyClassTypeEnum.PDF;
            case 'zip':
                return Enums.StudyClassTypeEnum.H5;
            case 'mp3':
                summer.init('audioContent');
                return Enums.StudyClassTypeEnum.AUDIO;
            case 'mp4':
                return Enums.StudyClassTypeEnum.VIDEO;
        }
        return null;
    }
});